package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Menu;
import cn.kgc.crowdfundingadmin.pojo.Tag;
import cn.kgc.crowdfundingadmin.service.MenuService;
import cn.kgc.crowdfundingadmin.service.TagService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈啸
 * @create 2020-02-19 16:05
 */
@RestController
public class TagController {

    @Resource
    TagService tagService;

    @RequestMapping("/tag/get/whole/tree")
    public ResultEntity<Tag> getWholeTree(){
// 1.查询所有的树形节点用于组装
        List<Tag> tagList = tagService.getAll();
        // 2.将List<Menu>转换为Map<Menu的id,Menu>
        Map<Integer,Tag> tagMap = new HashMap<>();
        for (Tag tag : tagList) {
            tagMap.put(tag.getId(),tag);
        }

        // 3.声明变量用于存储根节点对象
        Tag rootNode = null;

        for (Tag tag : tagList) {
            //是父节点
            if (tag.getPid()==null){
                rootNode=tag;
                continue;
            }
            //是子节点从menuMap找到父节点，并添加到父节点的子节点集合中
            tagMap.get(tag.getPid()).getChildren().add(tag);
        }
        return ResultEntity.successWithoutData(rootNode);
    }
}